<?php
require('init.php');

if($_POST)
{
	if( $_POST['cm'] == '1' )
	{
		$time = now();
		$in = "INSERT INTO `sub_tempo` (`cid`, `teamid`,`probid`, `time_init`) 
			values('".$_POST['cid']."','".$teamdata['login']."','".$_POST['pb']."', '$time')
			ON DUPLICATE KEY UPDATE `time_init`='$time'";
		mysql_select_db($db2,$conn_db2);
		$res = mysql_query($in, $conn_db2);
		if(!$res)
		{
			throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conn_db2) . ", query: '$in'");
		}
	}
	if( $_POST['cm'] == '3' )
	{
		$time_s = now();
		$sel = "SELECT `time_init` FROM `sub_tempo` WHERE `cid`='".$_POST['cid']."' 
			AND `teamid`='".$teamdata['login']."' AND `probid`='".$_POST['prob']."' ";
		mysql_select_db($db2,$conn_db2);
		$r = mysql_query($sel, $conn_db2);
		$times = mysql_fetch_array($r, MYSQL_BOTH);
		$nrows = mysql_num_rows($r);
		if(!$r)
		{
			throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conn_db2) . ", query: '$sel'");
		}
		if($nrows == 0)
		{
			$in = "INSERT INTO `sub_tempo` (`cid`, `teamid`,`probid`, `time_init`) 
		values('".$_POST['cid']."','".$teamdata['login']."','".$_POST['prob']."', '$time_s')";
			mysql_select_db($db2,$conn_db2);
			$res = mysql_query($in, $conn_db2);
			if(!$res)
			{
				throw new RuntimeException("SQL syntax-error, ". "Error " .
					mysql_error($conn_db2) . ", query: '$in'");
			}
		}else{
			$in = "INSERT INTO `sub_tempo` (`cid`, `teamid`,`probid`, `time_init`) 
			values('".$_POST['cid']."','".$teamdata['login']."','".$_POST['prob']."', '$time_s')
			ON DUPLICATE KEY UPDATE `time_init`='$time_s'";
			mysql_select_db($db2,$conn_db2);
			$res = mysql_query($in, $conn_db2);
			if(!$res)
			{
				throw new RuntimeException("SQL syntax-error, ". "Error " .
				mysql_error($conn_db2) . ", query: '$in'");
			}
			}
	}
}

?>
